<template>
  <div class="hazTree">
    <div class="haz-header" v-if="!hideTitle">
      调查对象分类
    </div>
    <div>
      <el-tree class="filter-tree"
            :data="treeData"
            :props="defaultProps"
              @node-contextmenu="handleNodeContextMenu"
            default-expand-all highlight-current :expand-on-click-node="false"
            check-on-click-node
            @check="categoryCheckHandler"
        ref="tree">
          <template #default="{ data }">
          <el-tooltip :content="data.label" placement="top">
            <span class="tree-node-label">{{ data.label }}</span>
          </el-tooltip>
        </template>
      </el-tree>
        <el-popover
        v-model="showMenu"
        :style="{ top: `${menuPosition.y}px`, left: `${menuPosition.x}px`,position:'absolute' }"
        trigger="manual"
        @click="closeMenu"
    >
      <div class="p-menu">
        <div class="p-menu-item" @click="downloadHandler">
          批量下载模板
        </div>
      </div>
    </el-popover>
    </div>
  </div>
</template>

<script lang="ts">
import {Component, Vue,Prop,Emit} from 'vue-property-decorator';
import {
  getStartWorkCategoryTree
} from '@/api/investWork';

@Component({
  components: {}
})
export default class HazTree extends Vue {
  @Prop()
  private hideTitle?:boolean;
  @Prop()
  private isEdit!: any;
  currentNode:any;
  private treeData: any = [];
  showMenu = false;
  menuPosition = { x: 0, y: 0 };
  private defaultProps: any = {
    children: 'children',
    label: 'label'
  }
  categoryCheckHandler(data: any) {
    this.$emit('selected', data);
    this.closeMenu()
  }

  getCategoryTree(bigCategoryId?:number){
    getStartWorkCategoryTree({bigCategoryId: bigCategoryId}).then(res =>{
      if(res){
          this.treeData = res.data;
      }
    });
  }

  handleNodeContextMenu(event, data) {
    if(this.isEdit){
      event.preventDefault();
      this.currentNode = data;
      this.showMenu = true;
      this.menuPosition = { x: event.clientX, y: event.clientY };
    }
  }

  @Emit('downTemplateHandler')
  downloadHandler(){
    this.showMenu = false;
    return this.currentNode;
  }

  closeMenu() {
    this.showMenu = false;
  }

  mounted(){
    this.getCategoryTree();
    (this as any).$bus.$on('categoryCheckedHandler',(item:any) =>{

        this.getCategoryTree(item.id);
    });

  }

  


}
</script>
<style lang="scss" scoped>
.hazTree{
  width: 260px;
  background-color: #ffffff;
  padding: 12px 0;
  .haz-header{
    height: 28px;
    line-height: 28px;
    color: #448f7d;
    padding-left: 18px;
    border-left: 3px solid #448f7d;
    font-size: 14px;
    font-weight: bold;
    background: linear-gradient(to right, #e1f5f1, rgba(255, 255, 255, 1));
  }
}
.tree-node-label {
  display: inline-block;
  max-width: 200px; /* 根据实际容器宽度调整 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
